<?php
declare(ENCODING = 'utf-8');

/*
 * CR_StorageAccessTestBase.php created on 14 avr. 08 
 * Author: Rodrigue Le Gall
 * 
 * This file is inspired by the project Typo3
 *
 * RPortal is an open source web based Content Management System
 * Copyright (C) 2002-2008  Rodrigue Le Gall
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

require_once dirname(__FILE__).'/CR_Includes.php';

/**
 * Tests for the StorageAccess implementation. Needs to be extended
 * for various storage types
 *
 * @package CR
 * @subpackage Tests
 */
class CR_StorageAccessTestBase extends PHPUnit_Framework_TestCase {

	/**
	 * @var CR_StorageAccessInterface
	 */
	protected $storageAccess;

	/**
	 * Checks if we can store and remove a raw node properly.
	 * @test
	 */
	public function addNodeAndRemoveNodeWork() {
		$UUID = Injector::getObject('UUIDGenerator')->generateUUID();
		$expectedRawNode = array(
			'id' => NULL,
			'pid' => '96bca35d-1ef5-4a47-8b0c-0bfc69507d00',
			'name' => 'TestNode1',
			'uuid' => $UUID,
			'nodetype' => '1'
		);
		$this->storageAccess->addNode($UUID, '96bca35d-1ef5-4a47-8b0c-0bfc69507d00', '1', 'TestNode1');
		$rawNode = $this->storageAccess->getRawNodeByUUID($UUID);
		$this->assertTrue(is_array($rawNode), 'getRawNodeByUUID() did not return an array for a just created node entry.');
		$expectedRawNode['id'] = $rawNode['id'];
		$this->assertSame($expectedRawNode, $rawNode, 'The returned raw node had not the expected values.');

		$this->storageAccess->removeNode($UUID);
		$rawNode = $this->storageAccess->getRawNodeByUUID($UUID);
		$this->assertFalse($rawNode, 'getRawNodeByUUID() did return an array for a just removed node entry.');
	}

	/**
	 * Checks if we can update and remove a raw node properly
	 * @test
	 */
	public function updateNodeAndRemoveNodeWork() {
		$UUID = Injector::getObject('UUIDGenerator')->generateUUID();
		$expectedRawNode = array(
			'id' => NULL,
			'pid' => '96bca35d-1ef5-4a47-8b0c-0bfc69507d00',
			'name' => 'TestNode2',
			'uuid' => $UUID,
			'nodetype' => '1'
		);
		$this->storageAccess->addNode($UUID, '96bca35d-1ef5-4a47-8b0c-0bfc69507d00', '1', 'TestNode2');
		$rawNode = $this->storageAccess->getRawNodeByUUID($UUID);
		$this->assertTrue(is_array($rawNode), 'getRawNodeByUUID() did not return an array for a just created node entry.');
		$expectedRawNode['id'] = $rawNode['id'];
		$this->assertSame($expectedRawNode, $rawNode, 'The returned raw node had not the expected values.');

		$expectedRawNodeUpdated = array(
			'id' => $rawNode['id'],
			'pid' => '96bca35d-1ef5-4a47-8b0c-0bfc69507d01',
			'name' => 'TestNode2Updated',
			'uuid' => $UUID,
			'nodetype' => '3'
		);
		$this->storageAccess->updateNode($UUID, '96bca35d-1ef5-4a47-8b0c-0bfc69507d01', '3', 'TestNode2Updated');
		$rawNodeUpdated = $this->storageAccess->getRawNodeByUUID($UUID);
		$this->assertTrue(is_array($rawNodeUpdated), 'getRawNodeByUUID() did not return an array for an updated node entry.');
		$this->assertSame($expectedRawNodeUpdated, $rawNodeUpdated, 'The returned raw node had not the expected (updated) values.');

		$this->storageAccess->removeNode($UUID);
		$rawNode = $this->storageAccess->getRawNodeByUUID($UUID);
		$this->assertFalse($rawNode, 'getRawNodeByUUID() did return an array for a just removed node entry.');
	}

}
